package com.kevin.springboot.demos.service.impl;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.io.font.FontProgram;
import com.itextpdf.io.font.FontProgramFactory;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.layout.font.FontProvider;
import com.itextpdf.layout.font.FontSet;
import com.kevin.springboot.demos.domain.OrderInfo;
import com.kevin.springboot.demos.exception.ServiceException;
import com.kevin.springboot.demos.service.OrderService;
import com.kevin.springboot.demos.utils.DateUtil;
import com.kevin.springboot.demos.utils.FreemarkerUtil;
import com.kevin.springboot.demos.utils.HtmlConvertPdfUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import static com.itextpdf.html2pdf.HtmlConverter.convertToPdf;
import static com.itextpdf.styledxmlparser.css.CommonCssConstants.FONT;

/**
 * 订单服务实现
 * @author: wangyong
 * @date: 2019/8/17 12:44
 */
@Service
public class OrderServiceImpl implements OrderService {

	private static final Logger logger = LoggerFactory.getLogger(OrderService.class);


	@Override
	public List<OrderInfo> getOrderList(String limit) {
		OrderInfo orderInfo = null;
		List<OrderInfo> orderInfoList = Lists.newArrayListWithCapacity(200);
		int size=Strings.isNullOrEmpty(limit)?10:Integer.parseInt(limit);

		for (int i = 0; i < size; i++) {
			orderInfo = new OrderInfo();
			orderInfo.setOrderNo(UUID.randomUUID().toString().replace("-",""));
			orderInfo.setMerchantOrderNo(DateUtil.getDateTimeStr(new Date(), DateUtil.YYYYMMDDHHMMSS));
			orderInfo.setPayeeName("张三");
			orderInfo.setPayeeAccount("12312423334234");
			orderInfo.setAmount("100.00");
			orderInfo.setBankName("工商银行");
			orderInfo.setMerchantNo("10000000147");
			orderInfo.setMerchantName("xxx公司");
			orderInfo.setBatchNo(DateUtil.getDateStr(new Date(), DateUtil.YYYYMMDD));
			orderInfo.setPayDate(DateUtil.getDateTimeStr(new Date(), DateUtil.YYYYMMDDHHMMSS));
			orderInfo.setCreateDate(DateUtil.getDateTimeStr(new Date(), DateUtil.YYYYMMDDHHMMSS));
			orderInfoList.add(orderInfo);
		}
		return orderInfoList;
	}


	@Override
	public void getMerchantVoucher(List<OrderInfo> orderInfoList,OutputStream outputStream)throws ServiceException {
		try {
			if (CollectionUtils.isEmpty(orderInfoList)) {
				return;
			}
			String html = FreemarkerUtil.render(null, "orderInfo.ftl");
			HtmlConvertPdfUtil.convert(html,outputStream,"订单凭证");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}



}
